package com.example.comment.controller;

import com.alibaba.fastjson.JSON;
import com.example.comment.pojo.Review;
import com.example.comment.service.ReviewService;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.usian.model.common.dtos.ResponseResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class ReviewController {
    @Autowired
    ReviewService reviewService;
    @GetMapping("get_all_review")
    @ApiOperation(nickname = "查看所有评论信息",value="查看所有评论信息")
    public ResponseResult getAllReview(){
        List<Review> list =  reviewService.findAllReview();
        log.info("===========>您要查看的评论信息为:{}",list);
        return ResponseResult.okResult(list);
    }
    @GetMapping("find_review_by_name")
    @ApiOperation(nickname = "根据用户名精确查询",value="根据用户名精确查询")
    @ApiImplicitParam(name = "username",value="用户名")
    public ResponseResult findReviewByName(String username){
        List<Review> list = reviewService.findReviewByName(username);
        if (CollectionUtils.isEmpty(list)){
            return ResponseResult.errorResult(400,"您要查询的用户评论不存在");
        }
        log.info("=============>您查询到评论信息为:{}",list);
        return ResponseResult.okResult(list);
    }
    @GetMapping("find_review_query_by_name")
    @ApiOperation(nickname = "根据用户名模糊查询",value="根据用户名模糊查询")
    @ApiImplicitParam(name = "username",value = "用户名")
    public ResponseResult findReviewQueryByName(@RequestParam String username){
        List<Review> list = reviewService.findReviewQueryByName(username);
        if (CollectionUtils.isEmpty(list)){
            return ResponseResult.errorResult(400,"您要查询的用户名不存在");
        }
        log.info("=============>您查询到评论信息为:{}",list);
        return ResponseResult.okResult(list);
    }
    @ApiOperation(nickname = "添加评论",value="添加评论")
    @PostMapping("add_review")
    public ResponseResult addReview(@RequestBody @Validated Review review){
        Review returnReview = reviewService.addReview(review);
        if (StringUtils.isEmpty(returnReview)){
            log.info("===========>添加失败");
            return ResponseResult.errorResult(400,"添加失败");
        }
        log.info("===========>您要添加的评论信息为:{}",returnReview);
        return ResponseResult.okResult(200,"添加成功");
    }
    @ApiOperation(nickname = "修改评论信息",value = "修改评论信息")
    @PostMapping("update_review")
    public ResponseResult updateReview(@RequestBody @Validated Review review){
        System.out.println(review);
        UpdateResult result = reviewService.updateReview(review);
        if (result.getMatchedCount()==0){
            log.info("===========>该评论已不存在");
            return ResponseResult.errorResult(400,"该评论已不存在");
        }else if (result.getModifiedCount()==0){
            log.info("============>修改失败");
            return ResponseResult.okResult("修改失败");
        }else {
            log.info("=============>修改的用户信息为:{}", JSON.toJSONString(review));
            return ResponseResult.okResult("修改成功");
        }
    }

    @GetMapping("delete_review_by_name")
    @ApiOperation(nickname = "根据名称进行删除",value = "根据名称进行删除")
    public ResponseResult deleteReviewByName(String username){
        DeleteResult result = reviewService.deleteReviewByName(username);
        if (result.getDeletedCount()==0){
            return ResponseResult.errorResult(400,"删除失败");
        }
        log.info("==============>删除的评论信息为:{}",username);
        return ResponseResult.okResult("删除成功");
    }
    
}
